package com.example.weather_plus.ui.weatherforecast.viewmodel;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.weather_plus.data.model.AQIInfo;
import com.example.weather_plus.data.model.CityInfo;
import com.example.weather_plus.data.model.DailyForecastInfo;
import com.example.weather_plus.data.model.HourlyForecastInfo;
import com.example.weather_plus.data.model.WeatherInfo;
import com.example.weather_plus.data.repository.GeoRepository;
import com.example.weather_plus.data.repository.WeatherRepository;


public class WeatherViewModel extends AndroidViewModel {
    private final MutableLiveData<WeatherInfo> weatherText = new MutableLiveData<>();
    private final MutableLiveData<DailyForecastInfo> forecastPost = new MutableLiveData<>();
    private final MutableLiveData<AQIInfo> aqiInfo = new MutableLiveData<>();
    private final MutableLiveData<HourlyForecastInfo> hourly = new MutableLiveData<>();
    private final MutableLiveData<String> locationLiveData = new MutableLiveData<>();
    private final MutableLiveData<CityInfo> geoInfo = new MutableLiveData<>();
    private final WeatherRepository repository;
    private final GeoRepository geo_repository;

    private String location = "101010100";
    private final String apiKey = "7c55e04213a340cc87f79b837edfc53b";

    public WeatherViewModel(@NonNull Application application) {
        super(application);
        repository = new WeatherRepository();
        geo_repository = new GeoRepository();

        locationLiveData.observeForever(newLocation -> {
            if (!TextUtils.isEmpty(newLocation)) {
                location = newLocation; // 更新location属性
                fetchCurrentWeather(); // 位置变化时重新获取天气
                fetchDailyWeatherForecast();
                fetchCurrentAQI();
                fetchHourlyForecast();
                fetchGeoInfo();
            }
        });
    }

    public void fetchCurrentWeather() {
        // 直接使用WeatherRepository的LiveData，无需自定义Executor
        LiveData<WeatherInfo> weatherLiveData = repository.fetchCurrentWeather(location, apiKey);

        weatherLiveData.observeForever(weatherInfo -> {
            if (weatherInfo != null) {
                weatherText.postValue(weatherInfo);
            } else {
                Log.e("WeatherViewModel", "Received null WeatherInfo");
            }
        });
    }

    public void fetchDailyWeatherForecast() {
        LiveData<DailyForecastInfo> weatherDailyForecast = repository.fetchForecast(location, apiKey);

        weatherDailyForecast.observeForever(forecastInfo -> {
            if (forecastInfo != null) {
                // 假设你想要提取WeatherInfo中的某个字段转换为String
                forecastPost.postValue(forecastInfo);
            } else {
                Log.e("WeatherViewModel", "Received null WeatherInfo");
            }
        });
    }

    public void fetchCurrentAQI() {
        LiveData<AQIInfo> aqi = repository.fetchAQI(location, apiKey);

        aqi.observeForever(info -> {
            if (info != null) {
                aqiInfo.postValue(info);
            } else {
                Log.e("WeatherViewModel", "Received null AQIInfo");
            }
        });
    }

    public void fetchHourlyForecast() {
        LiveData<HourlyForecastInfo> cast = repository.fetchHourlyForecast(location, apiKey);

        cast.observeForever(info -> {
            if (info != null) {
                // 假设你想要提取WeatherInfo中的某个字段转换为String
                hourly.postValue(info);
            } else {
                Log.e("DDDD", "Received null AQIInfo");
            }
        });
    }

    public void fetchGeoInfo() {
        LiveData<CityInfo> geo = geo_repository.fetchCityInfo(location, apiKey);

        geo.observeForever(info -> {
            if (info != null) {
                geoInfo.postValue(info);
            } else {
                Log.e("GGGG", "Received null GGGG");
            }
        });
    }

    public LiveData<WeatherInfo> getCurrentWeather() {
        fetchCurrentWeather();
        return weatherText;
    }

    public LiveData<DailyForecastInfo> getDailyWeatherForecast() {
        fetchDailyWeatherForecast();
        return forecastPost;
    }

    public LiveData<AQIInfo> getCurrentAQI() {
        fetchCurrentAQI();
        return aqiInfo;
    }

    public LiveData<HourlyForecastInfo> getHourlyForecast() {
        fetchHourlyForecast();
        return hourly;
    }

    public LiveData<CityInfo> getCityInfo() {
        fetchGeoInfo();
        return geoInfo;
    }

    public void setLocation(String set) {
        locationLiveData.postValue(set); // 使用postValue更新LiveData，确保在主线程上操作
    }

}


//public class WeatherViewModel extends ViewModel {
//    private final WeatherRepository weatherRepository;
//
//    private MutableLiveData<WeatherInfo> currentWeather = new MutableLiveData<>();
//    private MutableLiveData<ForecastInfo> forecastInfo = new MutableLiveData<>();
//    private MutableLiveData<LocationInfo> locationInfo = new MutableLiveData<>();
//    private MutableLiveData<AQIInfo> aqiInfo = new MutableLiveData<>();
//
//    public WeatherViewModel(WeatherRepository weatherRepository) {
//        this.weatherRepository = weatherRepository;
//    }
//
//    // LiveData getters
//    public LiveData<WeatherInfo> getCurrentWeather() { return currentWeather; }
//    public LiveData<ForecastInfo> getForecastInfo() { return forecastInfo; }
//    public LiveData<LocationInfo> getLocationInfo() { return locationInfo; }
//    public LiveData<AQIInfo> getAQIInfo() { return aqiInfo; }
//
//    private final CoroutineScope viewModelScope = MainScope().plus(SupervisorJob());
//
//    public void fetchWeather(String locationId) {
//        viewModelScope.launch {
//            try {
//                // 这里调用你的Repository或其他数据获取方法
//                // 例如: WeatherInfo weatherInfo = weatherRepository.fetchWeatherInfo(locationId);
//
//                // 示例代码，实际应替换为上述注释中的逻辑
//                System.out.println("Fetching weather for " + locationId);
//
//                // 假定的异步操作完成
//            } catch (Exception e) {
//                // 处理异常
//                e.printStackTrace();
//            }
//        };
//    }
//
//    @Override
//    protected void onCleared() {
//        super.onCleared();
//        viewModelScope.coroutineContext.cancelChildren(); // 取消所有子Job，模拟viewModelScope的取消行为
//    }
//}